package com.sc.study.foundation.service.impl;

import com.sc.study.foundation.domain.ScSubjectChapter;
import com.sc.study.foundation.domain.ScSubjectTable;
import com.sc.study.foundation.mapper.ScSubjectChapterMapper;
import com.sc.study.foundation.mapper.ScSubjectTableMapper;
import com.sc.study.foundation.service.IScSubjectTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 课程Service业务层处理
 *
 * @author sc
 * @date 2022-02-27
 */
@Service
public class ScSubjectTableServiceImpl implements IScSubjectTableService {
    @Autowired
    private ScSubjectTableMapper scSubjectTableMapper;

    @Autowired
    private ScSubjectChapterMapper scSubjectChapterMapper;

    /**
     * 获取科目授课老师信息
     *
     * @return
     */
    public List<Map> selectSubjectClazzTeacher() {
        List<Map> maps = scSubjectTableMapper.selectSubjectClazzTeacher();
        return maps;
    }



    /**
     * 获取课表信息
     *
     * @param map 当前班级的课表信息
     * @return
     */
    public Map selectSubjectChapterValue(Map map) {
        try {
            Integer oldNum = (Integer) map.get("num");
            Integer subjectId = (Integer) map.get("subId");
            Integer clazzId = (Integer) map.get("clazzId");
            Integer num = (Integer) map.get("num");
            Integer subLeftOver = (Integer) map.get("subLeftOver"); //剩余课时
            Integer subTime = (Integer) map.get("subTime"); //总课时
            Integer subAtTime = (Integer) map.get("subAtTime"); //每次扣除课时
            Integer subjectTime = subTime - subLeftOver;//算出已经上了多少个课时
            num = subAtTime * num;//即将要上的课时数量
            ScSubjectChapter scSubjectChapter = scSubjectChapterMapper.selectSubjectChapterBySubId(subjectId, subjectTime, num);
            if (scSubjectChapter != null) {
                map.put("subChapterId", scSubjectChapter.getId());
                map.put("subChapterName", scSubjectChapter.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 当如果章节为空时获取新课程的章节
     *
     * @param value 第几次查询到空的情况
     * @param map   当前排课的班级信息
     * @return
     */
    public Map selectNextSubjectChapterNotNull(Integer value, Map map) {
        Integer subAtTime = (Integer) map.get("subAtTime"); //每次扣除课时
        Integer clazzId = (Integer) map.get("clazzId");
        value = value * subAtTime;
        //获取到下一个授课老师和授课的课程
        List<Map> nextSubject = scSubjectTableMapper.selectNextSubjectClazzTeacher(clazzId, value);
        if (nextSubject != null) {
            ScSubjectChapter scSubjectChapter = null;
            map = nextSubject.get(0);
            Integer subjectId = (Integer) map.get("subId");
            if (map != null && subjectId != null && subAtTime != null) {
                scSubjectChapter = scSubjectChapterMapper.selectSubjectChapterBySubId(subjectId, 0, value);
            }
            if (scSubjectChapter != null) {
                map.put("subChapterId", scSubjectChapter.getId());
                map.put("subChapterName", scSubjectChapter.getName());
            }
        }
        return map;
    }

    /**
     * 查询课程
     *
     * @param id 课程主键
     * @return 课程
     */
    @Override
    public ScSubjectTable selectScSubjectTableById(Long id) {
        return scSubjectTableMapper.selectScSubjectTableById(id);
    }

    /**
     * 查询课程列表
     *
     * @param scSubjectTable 课程
     * @return 课程
     */
    @Override
    public List<ScSubjectTable> selectScSubjectTableList(ScSubjectTable scSubjectTable) {
        return scSubjectTableMapper.selectScSubjectTableList(scSubjectTable);
    }

    /**
     * 新增课程
     *
     * @param scSubjectTable 课程
     * @return 结果
     */
    @Override
    public int insertScSubjectTable(ScSubjectTable scSubjectTable) {
        return scSubjectTableMapper.insertScSubjectTable(scSubjectTable);
    }

    /**
     * 修改课程
     *
     * @param scSubjectTable 课程
     * @return 结果
     */
    @Override
    public int updateScSubjectTable(ScSubjectTable scSubjectTable) {
        return scSubjectTableMapper.updateScSubjectTable(scSubjectTable);
    }

    /**
     * 批量删除课程
     *
     * @param ids 需要删除的课程主键
     * @return 结果
     */
    @Override
    public int deleteScSubjectTableByIds(Long[] ids) {
        return scSubjectTableMapper.deleteScSubjectTableByIds(ids);
    }

    /**
     * 删除课程信息
     *
     * @param id 课程主键
     * @return 结果
     */
    @Override
    public int deleteScSubjectTableById(Long id) {
        return scSubjectTableMapper.deleteScSubjectTableById(id);
    }
}
